package com.itheima.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;

/**
 * @author zhangyan
 * @date 2021/3/16 11:17
 */

@Configuration //指定为配置文件
public class CorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter() {

        CorsConfiguration config = new CorsConfiguration();

        //允许所有的方法
        config.addAllowedMethod("*");
        //运行所有的域进行请求
        config.addAllowedOrigin("*");
        //允许所有的请求头
        config.addAllowedHeader("*");

        // org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
        //
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());

        //配置可以访问的地址
        source.registerCorsConfiguration("/**", config); //针对所有的请求都支持跨域

        return new CorsWebFilter(source);
    }
}
